<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 400px;
            margin: 50px auto;
        }

        .table .item {
            float: left;
            width: 100px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: burlywood;
        }
        
    </style>
</head>

<body>
    <div id="app">
        <h3>直接输出数组{{list}}</h3>
        <h3>直接输出数组{{userList}}</h3>
        <!-- item就是集合中每一个元素 -->
        <button v-for="item in list">{{item}}</button>




        <div class="table">
            <div>
                用户名<input v-model="username" />
                <span>{{usernametip}}</span>
            </div>
            <div>
                年龄<input v-model="userage" />
            </div>
            <div>
                <button @click="add">新增</button>
            </div>
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">密码</div>
                <div class="item">操作</div>
            </div>
            <!-- item就是对象 -->
            <div class="tr" v-for="item in userList">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userage}}</div>
                <div class="item" @click="del(item.id)">删除</div>
            </div>
        </div>


    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username: "",
            usernametip:"",
            userage: "",
            list: [1, 2, 3, 4],
            userList: [{ id: 1, username: "leson", userage: 18 }, { id: 2, username: "lala", userage: 19 }, { id: 3, username: "lulu", userage: 17 }]
        },
        watch: {//监听
            username(newValue) { //当data中的username的值发生改变的时候就会触发
                // console.log(newValue)
                //判断用户名是否存在  判断newValue在userList中存不存在
                if(this.userList.some(item=>item.username==newValue)){
                    this.usernametip = "该用户已经存在";
                }else{
                    this.usernametip= "√";
                }



            },
            userList: {//多层监听
                deep: true,
                handler() {
                    //这个里面写
                }
            }

        },
        methods: {
            add() {
                // alert(this.username+"年龄"+this.userage);
                //如果保证id的正确性
                //id  应该是userList中id的最大值+1
                //找到userList中id 
                let ids = this.userList.map(item => item.id);//
                //利用map会产生一个新数组 [1,2,3]
                ids.sort((a, b) => b - a);//让数组倒叙排序 [3,2,1];
                //sort会影响原数组
                let id = ids[0] ? ids[0] + 1 : 1;
                this.userList.push({ id, username: this.username, userage: this.userage });
            },

            del(id) {
                this.userList = this.userList.filter(item => item.id != id);
                // this.userList = this.userList.filter(function(item){
                //     return  item.id !=id;
                // })

                // this.userList = this.userList.filter((item)=>{
                //     return  item.id!=id;
                // })
                //简写  只有一个参数  参数的括号可以省略
                // {}里面只有一行判断  可以省略讲  {}和return一起省略
                //filter不会影响原数组   会产生一个新的数字 所以我们需要重新赋值
            }
        }
    })
</script>

</html>